<?php
/**
 * Session handler.
 *
 * @category Zend
 * @package Disputeo
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 * @license GPL v3
 */

class Disputeo_Session extends Zend_Session
{
    const SESSION_NAMESPACE = 'Disputeo';
    
    /**
     * @var $obj Zend_Session_Namespace
     */
    protected $_namespace;
    
    /**
     * @var $obj Disputeo_Session
     */
    protected static $_instance;
    
    /**
     * Instantiate session
     */
    protected function __construct()
    {
        self::start();
        $this->_namespace = new Zend_Session_Namespace(self::SESSION_NAMESPACE);

        if (! isset($this->_namespace->initialized)) {
            self::regenerateId();
            $this->_namespace->initialized = true;
        }
    }
    
    /**
     * Get a session object
     * 
     * @return Disputeo_Session
     */
    public static function getSession()
    {
        if (! self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
    
    /**
     * Calls getSession() and sets usertype if not set
     */
    public static function begin()
    {
        $session = self::getSession();
        
        if (! $session->usertype) {
            $session->usertype = Disputeo_Acl::GUEST;
        }
    }
    
    /**
     * Make session last a month if true
     * 
     * @param boolean $remember
     */
    public function persist($remember = false)
    {
        if ($remember) {
            parent::rememberMe(2629743.83); //seconds
        }
    }

    /**
     * Get a session var
     * 
     * @param string $name
     * @return mixed
     */
    public function __get($name)
    {
        if (isset($this->_namespace->$name)) {
            return $this->_namespace->$name;
        }
        
        return null;
    }

    /**
     * Set a session var (name=>value)
     * 
     * @param string $name
     * @param mixed $value 
     */
    public function __set($name, $value)
    {
        $this->_namespace->$name = $value;
    }
    
    /**
     * Clear session
     */
    public function clear()
    {
        self::destroy(false);
    }
}
